/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package com.yasee.chronic.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

import com.yasee.chronic.R;
import com.yasee.chronic.adapter.UserDetailAdapter;
import com.yasee.chronic.model.User;
import com.yasee.chronic.model.WorkBean;
import com.yasee.chronic.util.MyUtils;

import zuo.biao.library.base.BaseModel;
import zuo.biao.library.base.BaseView;
import zuo.biao.library.ui.WebViewActivity;
import zuo.biao.library.util.CommonUtil;
import zuo.biao.library.util.Log;
import zuo.biao.library.util.StringUtil;

/**
 * 用户View
 *
 * @author Lemon
 * @use <br> UserView userView = new UserView(context, resources);
 * <br> adapter中使用:[具体参考.BaseViewAdapter(getView使用自定义View的写法)]
 * <br> convertView = userView.createView(inflater);
 * <br> userView.bindView(position, data);
 * <br> 或  其它类中使用:
 * <br> containerView.addView(userView.createView(inflater));
 * <br> userView.bindView(data);
 * <br> 然后
 * <br> userView.setOnDataChangedListener(onDataChangedListener);data = userView.getData();//非必需
 * <br> userView.setOnClickListener(onClickListener);//非必需
 */
public class UserView extends BaseView<WorkBean.Data> implements OnClickListener {
    private static final String TAG = "UserView";

    public UserView(Activity context, ViewGroup parent) {
        super(context, R.layout.item_work_bench, parent);
    }

	//-------title
    public LinearLayout llTile;
	public TextView tvBedNo;
	public TextView tvName;
	public TextView tvSex;
	public TextView tvAge;
	public TextView tvDoc;
	public TextView tvDisease;
	public LinearLayout llContent;
	//------------血糖
	public LinearLayout llSugar;
	public ImageView ivSugar;
	public TextView tvSugarValue;
	public TextView tvSugarTime;
	public TextView tvSugarTip;
	public TextView tvSugarPlan;

	// ------------血压
	public LinearLayout llPressure;
	public ImageView ivPressure;
	public TextView tvPressureValue;
	public TextView tvPressureTime;
	public TextView tvPressureTip;
	public TextView tvPressureHeart;
	//-----------尿酸
	public LinearLayout llUric;
	public ImageView ivUric;
	public TextView tvUricValue;
	public TextView tvUricTime;
	public TextView tvUricTip;
	//-----------血酮
	public LinearLayout llKetone;
	public ImageView ivKetone;
	public TextView tvKetoneValue;
	public TextView tvKetoneTime;
	public TextView tvKetoneTip;


    @SuppressLint("InflateParams")
    @Override
    public View createView() {
		llTile = findView(R.id.item_title);
		llContent = findView(R.id.item_content);
		tvBedNo = findView(R.id.item_bed_no);
		tvSex = findView(R.id.item_sex);
		tvName = findView(R.id.item_name);
		tvAge = findView(R.id.item_age);
		tvDoc = findView(R.id.item_doc);
		tvDisease = findView(R.id.item_disease);

		llSugar = findView(R.id.ll_blood_sugar);
		ivSugar = findView(R.id.iv_blood_sugar);
		tvSugarValue = findView(R.id.tv_blood_sugar_value);
		tvSugarTime = findView(R.id.tv_blood_sugar_time);
		tvSugarTip = findView(R.id.tv_blood_sugar_tip);
		tvSugarTip = findView(R.id.tv_blood_sugar_tip);
		tvSugarPlan = findView(R.id.tv_blood_sugar_plan);

		llPressure = findView(R.id.ll_blood_pressure);
		ivPressure = findView(R.id.iv_blood_pressure);
		tvPressureValue = findView(R.id.tv_blood_pressure_value);
		tvPressureTime = findView(R.id.tv_blood_pressure_time);
		tvPressureTip = findView(R.id.tv_blood_pressure_tip);
		tvPressureHeart = findView(R.id.tv_blood_pressure_heart);

		llUric = findView(R.id.ll_uric_acid);
		ivUric = findView(R.id.iv_uric_acid);
		tvUricValue = findView(R.id.tv_uric_acid_value);
		tvUricTime = findView(R.id.tv_uric_acid_time);
		tvUricTip = findView(R.id.tv_uric_acid_tip);

		llKetone = findView(R.id.ll_blood_ketone);
		ivKetone = findView(R.id.iv_blood_ketone);
		tvKetoneValue = findView(R.id.tv_blood_ketone_value);
		tvKetoneTime = findView(R.id.tv_blood_ketone_time);
		tvKetoneTip = findView(R.id.tv_blood_ketone_tip);

        return super.createView();
    }

    @Override
    public void bindView(WorkBean.Data data_) {
        super.bindView(data_ != null ? data_ : new WorkBean.Data());
		tvBedNo.setText(StringUtil.getTrimedString(data.getBedName()));
		tvName.setText(StringUtil.getTrimedString(data.getName()));
		tvSex.setText(StringUtil.getTrimedString(data.getSex()).equals("1")?"男":"女");
		tvAge.setText(StringUtil.getTrimedString(data.getAge()).isEmpty()?"-岁":StringUtil.getTrimedString(data.getAge())+"岁");
		tvDoc.setText("责任医生："+StringUtil.getTrimedString(data.getDoctorName()));
		tvDisease.setText(StringUtil.getTrimedString(data.getDiagnosis()));


		if(
				(data.getBloodGlucoseLatest() == null || (data.getBloodPressureLatest()!=null && data.getBloodGlucoseLatest().getAlarmType() == 0))
				&& ( data.getBloodPressureLatest() ==null || (data.getBloodPressureLatest()!= null &&data.getBloodPressureLatest().getLevel() == 0))
				&& ( data.getUricAcidLatest() ==null || (data.getUricAcidLatest()!= null &&data.getUricAcidLatest().getAlarmType() == 0))
				&& ( data.getKetoneLatest() ==null || (data.getKetoneLatest()!= null &&data.getKetoneLatest().getAlarmType() == 0))
		){
			llTile.setBackground(getResources().getDrawable(R.drawable.rounded_corners_top_blue));
			llContent.setBackground(getResources().getDrawable(R.drawable.rounded_corners_bottom_blue));
		}else{
			llTile.setBackground(getResources().getDrawable(R.drawable.rounded_corners_top_red));
			llContent.setBackground(getResources().getDrawable(R.drawable.rounded_corners_bottom_red));
		}

		llSugar.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

			}
		});
		tvSugarValue.setText(data.getBloodGlucoseLatest() == null ? "-" : StringUtil.getTrimedString(data.getBloodGlucoseLatest().getResult()));
		if(data.getBloodGlucoseLatest() == null){
			tvSugarValue.setText("-");
			tvSugarTip.setVisibility(View.INVISIBLE);
		}else{
			if(data.getBloodGlucoseLatest().getAlarmType() == 0){
				tvSugarValue.setTextColor(getColor(R.color.black));
				tvSugarTip.setVisibility(View.INVISIBLE);
			}else if(data.getBloodGlucoseLatest().getAlarmType() == 1){
				tvSugarValue.setTextColor(getColor(R.color.red));
				tvSugarTip.setVisibility(View.VISIBLE);
				tvSugarTip.setText("↓");
			}else{
				tvSugarValue.setTextColor(getColor(R.color.red));
				tvSugarTip.setVisibility(View.VISIBLE);
				tvSugarTip.setText("↑");
			}
			if(data.getBloodGlucoseLatest().getTestTime()==null){
				tvSugarTime.setText("");
			}else{
				tvSugarTime.setText(MyUtils.getDateTime(Long.parseLong(data.getBloodGlucoseLatest().getTestTime())));
			}

			tvSugarPlan.setText("已检/计划：" + data.getBloodGlucoseLatest().getTestCount() +"次/"+ data.getBloodGlucoseLatest().getPlanCount() +"次");
		}

		if(data.getBloodPressureLatest() == null){
			tvPressureValue.setText("-");
			tvPressureTip.setVisibility(View.INVISIBLE);
		}else{
			tvPressureValue.setText(StringUtil.getTrimedString(data.getBloodPressureLatest().getDiastolic()) + "/" + StringUtil.getTrimedString(data.getBloodPressureLatest().getSystolic()));
			if(data.getBloodPressureLatest().getLevel() == 0 || data.getBloodPressureLatest().getLevel() == 1){
				tvPressureValue.setTextColor(getColor(R.color.black));
				tvPressureTip.setVisibility(View.INVISIBLE);
			}else if(data.getBloodPressureLatest().getLevel() > 1){
				tvPressureValue.setTextColor(getColor(R.color.red));
				tvPressureTip.setVisibility(View.VISIBLE);
				tvPressureTip.setText("↑");
			}else{
				tvPressureValue.setTextColor(getColor(R.color.red));
				tvPressureTip.setVisibility(View.VISIBLE);
				tvPressureTip.setText("↓");
			}
			Log.e(TAG,"格式化：：：：：：：："+data.getBloodPressureLatest().getTestTime());
			if(data.getBloodPressureLatest().getTestTime()==null){
				tvPressureTime.setText("");
			}else{
				tvPressureTime.setText(MyUtils.getDateTime(Long.parseLong(data.getBloodPressureLatest().getTestTime())));
			}
			Integer heartRate = data.getBloodPressureLatest().getHeartRate();
			if(heartRate == null){
				tvPressureHeart.setText("-");
			}else{
				tvPressureHeart.setText(data.getBloodPressureLatest().getHeartRate()+ "");
			}
		}

		if(data.getUricAcidLatest() == null){
			tvUricValue.setText("-");
			tvUricTip.setVisibility(View.INVISIBLE);
			tvUricValue.setTextColor(getColor(R.color.black));
			tvUricTime.setText("");
		}else{
			if(data.getUricAcidLatest().getAlarmType() == 0){
				tvUricValue.setTextColor(getColor(R.color.black));
				tvUricTip.setVisibility(View.INVISIBLE);
			}else if(data.getUricAcidLatest().getAlarmType() == 1){
				tvUricValue.setTextColor(getColor(R.color.red));
				tvUricTip.setVisibility(View.VISIBLE);
				tvUricTip.setText("↓");
			}else{
				tvUricValue.setTextColor(getColor(R.color.red));
				tvUricTip.setVisibility(View.VISIBLE);
				tvUricTip.setText("↑");
			}
			tvUricValue.setText(data.getUricAcidLatest().getResult()==null? "-" : data.getUricAcidLatest().getResult()+"");
			if(data.getUricAcidLatest().getTestTime()==null){
				tvUricTime.setText("");
			}else{
				tvUricTime.setText(MyUtils.getDateTime(Long.parseLong(data.getUricAcidLatest().getTestTime()+ "")));
			}
		}

		if(data.getKetoneLatest() == null){
			tvKetoneValue.setText("-");
			tvKetoneTip.setVisibility(View.INVISIBLE);
			tvKetoneValue.setTextColor(getColor(R.color.black));
			tvKetoneTime.setText("");
		}else{
			if(data.getKetoneLatest().getAlarmType() == 0){
				tvKetoneValue.setTextColor(getColor(R.color.black));
				tvKetoneTip.setVisibility(View.INVISIBLE);
			}else if(data.getKetoneLatest().getAlarmType() == 1){
				tvKetoneValue.setTextColor(getColor(R.color.red));
				tvKetoneTip.setVisibility(View.VISIBLE);
				tvKetoneTip.setText("↓");
			}else{
				tvKetoneValue.setTextColor(getColor(R.color.red));
				tvKetoneTip.setVisibility(View.VISIBLE);
				tvKetoneTip.setText("↑");
			}
			tvKetoneValue.setText(data.getKetoneLatest().getResult()==null? "-" : data.getKetoneLatest().getResult()+"");
			if(data.getKetoneLatest().getTestTime()==null){
				tvKetoneTime.setText("");
			}else {
				tvKetoneTime.setText(MyUtils.getDateTime(Long.parseLong(data.getKetoneLatest().getTestTime()+ "")));
			}
		}

    }

    @Override
    public void onClick(View v) {
//        if (BaseModel.isCorrect(data) == false) {
//            return;
//        }
        switch (v.getId()) {
            case R.id.ivUserViewHead:
//                toActivity(WebViewActivity.createIntent(context, data.getName(), data.getHead()));
                break;
            default:
                switch (v.getId()) {
                    case R.id.ivUserViewStar:
//                        data.setStarred(!data.getStarred());
                        break;
                    case R.id.tvUserViewSex:
//                        data.setSex(data.getSex() == User.SEX_FEMALE ? User.SEX_MAIL : User.SEX_FEMALE);
                        break;
                }
                bindView(data);
                break;
        }
    }
}